import React from 'react';
import { Link } from 'react-router';
import { Button, Form, Input, Row, Col,message } from 'antd';
import { connect } from 'react-redux'

import classNames from 'classnames';
import { putPW,_validate,changePage } from '../../store/actions';
const createForm = Form.create;
const FormItem = Form.Item;
function noop() {
    return false;
}
class _pwForm extends React.Component{
    constructor(props){
        super(props);
        this.state = {

            passBarShow: false, // 是否显示密码强度提示条
            rePassBarShow: false,
            passStrength: 'L', // 密码强度
            rePassStrength: 'L'
        };
        this.getPassStrenth = this.getPassStrenth.bind(this);
        this.renderPassStrengthBar = this.renderPassStrengthBar.bind(this);
        this.checkPass = this.checkPass.bind(this);
        this.checkPass2 = this.checkPass2.bind(this);
        this.handleSubmit = this.handleSubmit.bind(this);
        this.resetFields = this.resetFields.bind(this);
    }
    componentWillMount(){
        this.props.dispatch(changePage('reset-pw'))
    }
    getPassStrenth(value, type) {
        if (value) {
            let strength;
            // 密码强度的校验规则自定义，这里只是做个简单的示例
            if (value.length < 6) {
                strength = 'L';
            } else if (value.length <= 9) {
                strength = 'M';
            } else {
                strength = 'H';
            }
            if (type === 'password') {
                this.setState({ passBarShow: true, passStrength: strength });
            } else {
                this.setState({ rePassBarShow: true, rePassStrength: strength });
            }
        } else {
            if (type === 'pass') {
                this.setState({ passBarShow: false });
            } else {
                this.setState({ rePassBarShow: false });
            }
        }
    }

    checkPass(rule, value, callback) {
        const form = this.props.form;
        this.getPassStrenth(value, 'password');
        if (form.getFieldValue('password')) {
            form.validateFields(['rePass'], { force: true });
        }
        callback();
    }

    checkPass2(rule, value, callback) {
        const form = this.props.form;
        this.getPassStrenth(value, 'rePass');

        if (value && value !== form.getFieldValue('password')) {
            callback('两次输入密码不一致！');
        } else {
            callback();
        }
    }

    renderPassStrengthBar(type) {
        const strength = type === 'password' ? this.state.passStrength : this.state.rePassStrength;
        const classSet = classNames({
            'ant-pwd-strength': true,
            'ant-pwd-strength-low': strength === 'L',
            'ant-pwd-strength-medium': strength === 'M',
            'ant-pwd-strength-high': strength === 'H'
        });
        const level = {
            L: '低',
            M: '中',
            H: '高'
        };
        return (
            <div>
                <ul className={classSet}>
                    <li className="ant-pwd-strength-item ant-pwd-strength-item-1"> </li>
                    <li className="ant-pwd-strength-item ant-pwd-strength-item-2"> </li>
                    <li className="ant-pwd-strength-item ant-pwd-strength-item-3"> </li>
                    <span className="ant-form-text">
                        {level[strength]}
                    </span>
                </ul>
            </div>
        );
    }

    handleSubmit(e) {
        const _this = this;
        _validate(e,_this,function(values){
            const sendData = {
                old:values.oldPW,
                changeto:values.rePass
            };

            _this.props.dispatch(putPW(sendData,function(){
                message.success('修改密码成功，请牢记新密码',1);
                _this.resetFields()
            },function(res){
                message.error(res.head.msg,1);
                _this.resetFields()
            }))
        });
    }
    resetFields(){
        this.setState({
            passBarShow: false, // 是否显示密码强度提示条
            rePassBarShow: false
        },function(){
            this.props.form.resetFields()
        })
    }
    render(){
        const { getFieldProps } = this.props.form;
        const oldPassProps = getFieldProps('oldPW', {
            rules: [
                { required: true, min:6,max:20,whitespace: true,message: '密码为6-20位' },
                { validator: this.checkPass }
            ]
        });
        const passProps = getFieldProps('password', {
            rules: [
                { required: true, min:6,max:20,whitespace: true,message: '密码为6-20位' },
                { validator: this.checkPass }
            ]
        });
        const rePassProps = getFieldProps('rePass', {
            rules: [{
                required: true,
                whitespace: true,
                message: '请再次输入密码'
            }, {
                validator: this.checkPass2
            }]
        });
        const formItemLayout = {
            labelCol: { span: 6 },
            wrapperCol: { span: 18 }
        };
        return (
            <div>
                <Form horizontal form={this.props.form}>

                    <Row>
                        <Col span="18">
                            <FormItem
                                {...formItemLayout}
                                label="原始密码"
                            >
                                <Input {...oldPassProps}
                                    type="password"
                                    onContextMenu={noop} onPaste={noop} onCopy={noop} onCut={noop}
                                    autoComplete="off"
                                    id="oldPW"
                                />
                            </FormItem>
                        </Col>
                    </Row>

                    <Row>
                        <Col span="18">
                            <Col span="6"> </Col>
                            <Col span="18" style={{padding:'0 0 20px'}}>设置新的密码</Col>
                        </Col>
                    </Row>
                    <Row>
                        <Col span="18">
                            <FormItem
                                {...formItemLayout}
                                label="新密码"
                            >
                                <Input {...passProps} type="password"
                                                      onContextMenu={noop} onPaste={noop} onCopy={noop} onCut={noop}
                                                      autoComplete="off" id="password"
                                />
                            </FormItem>
                            <FormItem
                                {...formItemLayout}
                                label="确认新密码"
                            >
                                <Input {...rePassProps} type="password"
                                    onContextMenu={noop} onPaste={noop} onCopy={noop} onCut={noop}
                                    autoComplete="off" id="rePass"
                                />
                            </FormItem>
                        </Col>
                        <Col span="6">
                            {this.state.passBarShow ? this.renderPassStrengthBar('pass') : null}
                        </Col>
                    </Row>
                    <Row>
                        <Col span="18">
                            <Col span="18" offset="6">
                                <Button type="primary" onClick={this.handleSubmit}>修改密码</Button>
                                &nbsp;&nbsp;&nbsp;&nbsp;
                                <Button onClick={this.resetFields}>重置</Button>
                            </Col>
                        </Col>
                    </Row>
                </Form>

            </div>
        )

    }
}

const __pwForm = createForm()(_pwForm);

class _ResetPw extends React.Component{
    constructor(props){
        super(props)
    }
    render(){
        return(
            <div id="">
                <__pwForm user = {this.props.user} dispatch = {this.props.dispatch}/>
            </div>
        )
    }
}
let ResetPw;
export default ResetPw = connect((state) => {
    const {user} = state.rootReducer;
    return {user}
})(_ResetPw)
